﻿package birdry.game.sadari
{
	
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;
	
	
	/**
	* ...
	* @author birdry80
	*/
	public class SadariEngine 	{
		
		private var _height:Number = 500;
		private var _width:Number = 700;
		private var _rowLineMax:uint;
		private var _colLineMax:uint;
		private var _rowLineMargin:Number;
		private var _colLineMargin:Number;
		private var _sadariBaseLineArr:Array;
		private var _resultArr:Array;
		private var _resultPositionArr:Array;
		private var _sadariBaseLine:SadariBaseLine;
		
		/**
		 * 사다리게임 셋팅
		 * @param	colLineMax
		 * 세로라인의 행수 정의 (참여자수)
		 * @param	colLineMargin
		 * 라인의 가로간격
		 * @param	rowLineMargin
		 * 라인의 세로간격
		 */
		
		public function init(colLineMax:uint=3):void 		{
			_colLineMax = colLineMax;
			_sadariBaseLineArr = returnBasebaseLineArr();
			_resultArr = returnResultArr(_sadariBaseLineArr);
			_resultPositionArr = returnPositionArr(_resultArr);
			_sadariBaseLine.draw(baseLineArr, true);
		}
		
		public function showLine(): void	{
			_sadariBaseLine.draw(baseLineArr, false);
		}
		
		public function setPosition( x:Number, y:Number ): void	{
			_sadariBaseLine.view.x = x;
			_sadariBaseLine.view.y = y;
		}
		
		public function set bseLineColor(color:uint): void	{
			_sadariBaseLine.setLineColor(color);
		}
		
		public function set bseLineThickness(thickness:uint): void	{
			_sadariBaseLine.setLineThickness(thickness);
		}
		
		public function get width(): Number		{
			return (_resultArr.length-1) * _colLineMargin;
		}
		
		public function get height(): Number	{
			return _resultArr[0].length * _rowLineMargin;
		}
		
		public function get baseLine(): SadariBaseLine	{
			return _sadariBaseLine;
		}
		
		public function get baseLineArr():Array 	{
			return _sadariBaseLineArr;
		}
		
		public function get resultArr():Array 	{
			return _resultArr;
		}
		
		public function get resultPositionArr():Array 	{
			return _resultPositionArr;
		}
		
		/**
		 * 도출되는 배열의 확인
		 */
		public function traceArr():void 		{
			var i:uint, u:int;
			for ( i = 0; i < _sadariBaseLineArr.length; ++i)	{	trace(_sadariBaseLineArr[i]);	}
			trace("--------------------------------------");
			for ( i = 0; i < _resultArr.length; ++i)	{	trace(_resultArr[i]);	}
			trace("--------------------------------------");
			for ( i = 0; i < _resultPositionArr.length; ++i)	{	trace(_resultPositionArr[i]);	}
			trace("--------------------------------------");
		}
		
		/**
		 * 사다리게임 엔진 생성자
		 * @param	rowLineMax
		 * 가로라인의 열수 정의
		 */
		public function SadariEngine(container:DisplayObjectContainer, rowLineMax:uint = 7, colLineMargin:uint = 50, rowLineMargin:uint = 50)	{
			_rowLineMax = rowLineMax;
			_colLineMargin = colLineMargin;
			_rowLineMargin = rowLineMargin;
			_sadariBaseLine = new SadariBaseLine(colLineMargin, rowLineMargin);
			container.addChild(_sadariBaseLine.view);
		}
		
		private function returnBasebaseLineArr():Array 		{
			var baseLineArr:Array = new Array();
			for ( var i:uint = 0; i < _rowLineMax; ++i)	{
				baseLineArr[i] = [];
				for (var u:uint = 0; u < _colLineMax-1; ++u)	{
					baseLineArr[i][u] = getRandomInt( 0, 1 );
					if (u != 0 && baseLineArr[i][u - 1] != 0)	baseLineArr[i][u] = 0;
				}
			}
			return baseLineArr;
		}
		
		
		private function returnResultArr(baseLineArr:Array):Array 		{
			var tempArr:Array = [];
			_resultArr = [];
			for (var i:uint = 0; i < _colLineMax; ++i)	{
				tempArr[i] = i + 1;
				_resultArr[i] = [];
				_resultArr[i][0] = i + 1;
				for ( var u:uint = 0; u < _rowLineMax; ++u)	{
					if (tempArr[i] == 1)	{
						if (baseLineArr[u][tempArr[i] - 1] == 1)	{
							tempArr[i]++;
						}
					}	else if ( tempArr[i] == _colLineMax)	{
						if (baseLineArr[u][tempArr[i] - 2] == 1)	{
							tempArr[i]--;
						}
					}	else	{
						if (baseLineArr[u][tempArr[i] - 2] == 1)	{
							tempArr[i]--;
						}	else	if (baseLineArr[u][tempArr[i]-1] == 1)	{
							tempArr[i]++;
						}
					}
					_resultArr[i][u + 1] = tempArr[i];
				}
			}
			return _resultArr;
		}
		
		private function returnPositionArr(resultArr:Array):Array 		{
			_resultPositionArr = [];
			for (var i:uint = 0; i < _colLineMax; ++i)	{
				_resultPositionArr[i] = [];
				_resultPositionArr[i][0] = new Point( i * _colLineMargin, 0 );
				_resultPositionArr[i][1] = new Point( i * _colLineMargin, _rowLineMargin );
				var arrLength:uint;
				for (var u:uint = 0; u < _rowLineMax; ++u)	{
					if (resultArr[i][u] < resultArr[i][u + 1])	{
						arrLength = _resultPositionArr[i].length-1;
						_resultPositionArr[i].push( new Point( _resultPositionArr[i][arrLength].x + _colLineMargin, _resultPositionArr[i][arrLength].y) );
					}	else if (resultArr[i][u] > resultArr[i][u + 1])	{
						arrLength = _resultPositionArr[i].length-1;
						_resultPositionArr[i].push( new Point( _resultPositionArr[i][arrLength].x - _colLineMargin, _resultPositionArr[i][arrLength].y ) );
					}
					arrLength = _resultPositionArr[i].length-1;
					_resultPositionArr[i].push( new Point( _resultPositionArr[i][arrLength].x, _resultPositionArr[i][arrLength].y + _rowLineMargin ) );
				}
			}
			return _resultPositionArr;
		}
		
		private function getRandomInt(Mi:Number, Ma:Number):int  {   
			return Math.floor(Math.random()*(Ma-Mi+1))+Mi;
		}
		
	}	// class
	
}	// package